<?php

/**
 *
 * Copyright (c) 2011 Tomáš Holub (tomas.holub@webadvice.cz)
 * 
 * @author Tomáš Holub (tomas.holub@webadvice.cz)
 * @package OverFishCMS
 */

namespace AdminModule;

use OverFish, Nette, AdminModule\Forms\UserForm;

class UserPresenter extends BaseAdminPresenter {
    
    public $title = "Správa";
    private $id = False;
    protected $menuItem = "user";
    
    public function renderDefault() {
        $this->heading = "Uživatelé";
    }
    
    public function renderNew() {
        $this->heading = "Nový uživatel"; 
    }
    
    public function renderEdit($id) {
        $this->heading = "Upravit uživatele";
        $this->id = $id;
    }
    
    protected function createComponentUserForm($name)
    {
        $form = new UserForm($this, $name);
        
        if ($this->id) {
            $item = $this->repositories->user->find($this->id);
            
            $form->setDefaults(array(
                "id" => $item->getId(),
                "username" => $item->getUsername(),
                "fullname" => $item->getFullname(),
                "active" => $item->getActive()
            ));
        } else {
            $form->setDefaults(array(
                "active" => True,
            ));
        }
    }
    
    protected function createComponentGrid() {
        $datasource = $this->repositories->user->getDataSource();

        $grid = new OverFish\Controls\Grid();
        $grid->setDataSource($datasource);

        $grid->addColumn("fullname", "Jméno", array("sortable" => True));
        $grid->addColumn("username", "Uživatelské jméno", array("sortable" => True));
        $grid->addColumn("active", "Aktivní");
        $grid->addColumn("last_login", "Datum posledního přihlášení", array("sortable" => True));

        // action buttons
        $presenter = $this;
            
        $grid->addButton("edit", "Edit", array(
            "handler" => function ($user) use ($presenter) {
                $presenter->redirect(":Admin:User:edit", array("id" => $user->id));
            },
            "icon" => "icon-1"
        ));
            
            
        $grid->addButton("delete", "Delete", array(
            "handler" => function ($user) use ($presenter) {
                $presenter->redirect(":Admin:User:delete", array("id" => $user->id));
            },
            "icon" => "icon-2",
            "showText" => false,
        ));
            
        return $grid;
    }
}

?>
